package configuration

import (
	"encoding/json"
	"errors"
	"fmt"
	"kop/modules/configuration/client"
	"kop/modules/configuration/configuration/attribute/attrtype"
	"kop/util/log"
)

func init() {

	var err error
	if err = json.Unmarshal(client.Load("CaptainTemplate"), &CaptainConfigs); err != nil {
		panic(err)
	}

	log.Info("Load CaptainTemplate.json success.")

	//********************************************************************//

	if err = json.Unmarshal(client.Load("CaptainTalent"), &TalentConfigs); err != nil {
		panic(err)
	}

	log.Info("Load CaptainTalent.json success.")

	//********************************************************************//

	if err = json.Unmarshal(client.Load("SkillCaptain"), &Skill2Configs); err != nil {
		panic(err)
	}

	log.Info("Load SkillCaptain.json success.")

	//********************************************************************//

	if err = json.Unmarshal(client.Load("SkillTemplate"), &Skill1Configs); err != nil {
		panic(err)
	}

	log.Info("Load SkillTemplate.json success.")

	//********************************************************************//

	if err = json.Unmarshal(client.Load("SkillTarget"), &SkillTargetConfigs); err != nil {
		panic(err)
	}

	log.Info("Load SkillTarget.json success.")
}

type (
	// 船长
	CaptainConf struct {
		CaptainID      int32 `json:"ID"`
		Camp           int32
		Sex            int32
		AttributeID    int32
		Skill1         int32   // 主动技能
		Skill2         int32   // 被动技能1
		Skill3         int32   // 被动技能2
		ItemID         int32   // 船长碎片物品ID
		ItemNumber     int32   `json:"ItemCount"` // 船长碎片数量
		Talent         []int32 // 天赋
		FetterShip     int32   // 船只羁绊
		FetterMaid     int32   // 侍女羁绊
		FetterCaptains []int32 // 船长羁绊
	}

	// 天赋
	TalentConfig struct {
		TalentID    int32             `json:"ID"`
		Type        attrtype.AttrType // 1智力 2勇气 3魅力
		Value       int32             // 数值
		ItemID      int32             // 天赋书ID
		Probability float32           // 升级几率 n/10000
		PieceCount  int32             // 碎片升级需要的数量
	}

	// 技能1
	Skill1Config struct {
		SkillID       int32   `json:"ID"` // 技能ID
		Quality       int32   // 技能品质
		Probability   float32 // 释放概率
		TargetID      int32   // 目标ID
		EffectType    int32   // 效果类型
		AttributeType int32   // 属性类型
		ValueBase     float32 // 基础数值
		ValueInc      float32 // 增长数值
	}

	// 技能2 技能3号
	Skill2Config struct {
		SkillID     int32 `json:"ID"` // 技能ID
		Quality     int32 // 技能品质
		GradeMax    int32 // 最大等级
		AttributeID int32 // 属性ID
	}

	SkillTargetConfig struct {
		ID          int32 // 目标ID
		TargetType  int32 // 目标类型
		CampType    int32 // 阵营类型
		TargetCount int32 // 目标数量
	}
)

var (
	CaptainConfigs     = map[int32]CaptainConf{}
	TalentConfigs      = map[int32]TalentConfig{}
	Skill2Configs      = map[int32]Skill2Config{}
	Skill1Configs      = map[int32]Skill1Config{}
	SkillTargetConfigs = map[int32]SkillTargetConfig{}
)

func GetByCaptainID(captainID int32) (CaptainConf, error) {

	var conf, ok = CaptainConfigs[captainID]
	if ok {
		return conf, nil
	}

	return CaptainConf{}, errors.New(fmt.Sprintf("CaptainID not found [%d]", captainID))
}

func GetByTalentID(talentID int32) (TalentConfig, error) {

	var conf, ok = TalentConfigs[talentID]
	if ok {
		return conf, nil
	}

	return TalentConfig{}, errors.New(fmt.Sprintf("TalentID not found [%d]", talentID))
}

func GetBySkill2ID(ID int32) (Skill2Config, error) {

	var conf, ok = Skill2Configs[ID]
	if ok {
		return conf, nil
	}

	return Skill2Config{}, errors.New(fmt.Sprintf("Skill2ID not found [%d]", ID))
}

func GetBySkill1ID(ID int32) (Skill1Config, error) {

	var conf, ok = Skill1Configs[ID]
	if ok {
		return conf, nil
	}

	return Skill1Config{}, errors.New(fmt.Sprintf("Skill1ID not found [%d]", ID))
}

func GetBySkillTargetID(ID int32) (SkillTargetConfig, error) {

	var conf, ok = SkillTargetConfigs[ID]
	if ok {
		return conf, nil
	}

	return SkillTargetConfig{}, errors.New(fmt.Sprintf("SkillTargetID not found [%d]", ID))
}
